//
//  TTNetWorkTool.swift
//  NewWeiBo
//
//  Created by 吴天益 on 2017/7/25.
//  Copyright © 2017年 ttt. All rights reserved.
//

import UIKit
import AFNetworking

//枚举
enum RequestType{
    case get
    case post
}

class TTNetWorkTool: AFHTTPSessionManager {
 
    //制作单例
    static var shareTool: TTNetWorkTool = TTNetWorkTool()
    
    func request(type: RequestType, urlStr: String, parameters: Any, finishClosure: @escaping (Any?,Error?)->()){
        
        if type == .get{
            get(urlStr, parameters: parameters, progress: nil, success: { (_, response: Any?) in
                
                finishClosure(response,nil)
            }) { (_, error: Error) in
                
                finishClosure(nil,error)
            }
        }else{
            post(urlStr, parameters: parameters, progress: nil, success: { (_, response: Any?) in
                finishClosure(response,nil)
            }, failure: { (_, error: Error) in
                finishClosure(nil,error)
            })
        }
    }
    
    
    
    //MARK: - 用授权码请求令牌
    func requestLinPaiWithShouQuanMa(shouQuanMa: String?,backClosure: @escaping (Any?, Error?) -> ()) {
        
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = ["client_id": appKey,
                          "client_secret": appSecret,
                          "grant_type": "authorization_code",
                          "code": shouQuanMa,
                          "redirect_uri": redirect_uri]
        
        request(type: .post, urlStr: urlStr, parameters: parameters, finishClosure: backClosure)
    }
    
    
    //MARK: - 用令牌请求用户信息
    func requestUserMessageWithLinPai(linPai: String?,uid: Int64,backClosure: @escaping (Any?, Error?) -> ()
        ){
        
        let urlStr = "https://api.weibo.com/2/users/show.json"
        
        let parameters: [String: Any?] = ["access_token":linPai,
                          "uid": uid]
        
        request(type: .get, urlStr:urlStr, parameters: parameters, finishClosure: backClosure)
  
    }
    
    //请求关注用户微博
    func requestUserAndCareUsersMessageWithLinPai(linPai: String,sinceId: Int64 = 0,maxId: Int64 = 0, backClosure: @escaping (Any?, Error?) -> ()){
        
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameters: [String: Any?] = ["access_token":linPai,
                          "max_id": maxId,
                          "since_id": sinceId]
        
        request(type: .get, urlStr: urlStr, parameters: parameters, finishClosure: backClosure)
        
    }
    
    //发送微博(只有文字版本)
    func requestSendWordWeiBo(linPai: String,status: String,backClosure: @escaping (Any?, Error?) -> ()){
        
        let urlStr = "https://api.weibo.com/2/statuses/update.json"
        
        let parameters = ["access_token":linPai,
                          "status":status]
        
        request(type: .post, urlStr: urlStr, parameters: parameters, finishClosure: backClosure)
    }
    
    //发送微博(有文字有图片)
    func requestSendWordAndImageWeiBo(source: String,status: String,pic: UIImage,backClosure: @escaping (Any?, Error?) -> ()){
        
        let urlStr = "http://api.t.sina.com.cn/statuses/upload.(json%7cxml)"
        
        let parameters = ["source":source,
                          "status":status]
        //图片质量系数,越大越清晰
        let imageData = UIImageJPEGRepresentation(pic, 0.5)
        
        post(urlStr, parameters: parameters, constructingBodyWith: { (fromData) in
            //  withFileData: 图片二进制
            //  name: 服务端定义上传图片的参数
            //  fileName： 文件名
            //  mimeType: 资源类型
            fromData.appendPart(withFileData: imageData!, name: "pic", fileName: "ttt", mimeType: "image/jpeg")
        }, progress: nil, success: { (_, response) in
            backClosure(response,nil)
        }) { (_, error) in
            backClosure(nil,error)
        }
        
    }
    
    
    
    
    
    
    
}
